package cn.hit.edu.Servlet;

import java.io.*;

import cn.hit.edu.dao.UserDao;
import cn.hit.edu.dao.impl.UserDaoImpl;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import static java.lang.System.out;

@WebServlet(name = "loginServlet", value = "/login-servlet")
public class HelloServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        PrintWriter out = response.getWriter();
        UserDao userDao = new UserDaoImpl();
        if(username.equals("admin") && password.equals("123")){
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            String remberMe =request.getParameter("remberMe");
            if(remberMe != null){
                Cookie cookie = new Cookie("username",username);
                cookie.setMaxAge(60*60*24*7);
                response.addCookie(cookie);
            }
            response.sendRedirect("index.jsp");

        } else {
            response.sendRedirect("login.html");
        }


    }
}